package com.j.lemon.learn.flink.dataset;

import com.j.lemon.learn.flink.WordCount;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.aggregation.Aggregations;
import org.apache.flink.api.java.tuple.Tuple2;

import java.util.List;

/**
 * 将一组值聚合为单值，aggregate只能在tuple上操作,可以分组后进行sum、min、max聚合  ，可以进行多次聚合
 * @author lijunjun
 */
public class AggregateOperator {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSet<Tuple2<String,Integer>> dataSet = env.fromElements(
                new WordCount("zhangsan", 1),
                new WordCount("lisi", 2),
                new WordCount("lisi", 3))
                .map(new MapFunction<WordCount, Tuple2<String,Integer>>() {
                    @Override
                    public Tuple2<String,Integer> map(WordCount wordCount) throws Exception {
                        return new Tuple2<>(wordCount.getWord(), wordCount.getCount());
                    }
                }).groupBy(0).aggregate(Aggregations.SUM,1);
        dataSet.print();

    }
}
